package com.lenovo.manufacture;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class SaleAdapter extends BaseAdapter {
    private List<SellCar> sellCarList;
    private Context context;

    public SaleAdapter(List<SellCar> sellCarList, Context context) {
        this.sellCarList = sellCarList;
        this.context = context;
    }

    private class SaleHolder {
        private TextView CarName;
        private TextView CarPrice;
        private TextView CarSellTime;
        private TextView CarSellNum;
    }

    @Override
    public int getCount() {
        return sellCarList.size();
    }

    @Override
    public Object getItem(int position) {
        return sellCarList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SaleHolder saleHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.sale_list_item, parent, false
            );
            saleHolder = new SaleHolder();
            saleHolder.CarName = (TextView) convertView.findViewById(R.id.text_car_name);
            saleHolder.CarPrice = (TextView) convertView.findViewById(R.id.text_car_price);
            saleHolder.CarSellTime = (TextView) convertView.findViewById(R.id.text_car_sell_time);
            saleHolder.CarSellNum = (TextView) convertView.findViewById(R.id.text_car_sell_num);
            convertView.setTag(saleHolder);
        } else {
            saleHolder = (SaleHolder) convertView.getTag();
        }
        saleHolder.CarName.setText(sellCarList.get(position).getName());
        saleHolder.CarPrice.setText(String.valueOf(sellCarList.get(position).getPrice()));
        saleHolder.CarSellNum.setText(String.valueOf(sellCarList.get(position).getSellNum()));
        saleHolder.CarSellTime.setText(dateToString(sellCarList.get(position).getSellTime()));
        return convertView;
    }

    //Date转换为String
    public String dateToString(Date date) {
        return new SimpleDateFormat("yyyy-MM-dd").format(date);
    }
}
